package com.csu.tingtian.controller;

import com.csu.tingtian.common.CommonResponse;
import com.csu.tingtian.common.ResponseCode;
import com.csu.tingtian.entity.Account;
import com.csu.tingtian.entity.SignOn;
import com.csu.tingtian.entity.UnDeterminedFarms;
import com.csu.tingtian.persistence.AccountMapper;
import com.csu.tingtian.persistence.SignOnMapper;
import com.csu.tingtian.service.AccountService;
import com.csu.tingtian.utils.TokenUtil;
import com.csu.tingtian.vo.Business;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/account/")
public class AccountController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private SignOnMapper signOnMapper;

    //根据用户名获取单个用户信息
    @GetMapping("accounts/{username}/username")
    @ResponseBody
    public CommonResponse<Account> getAccount(@PathVariable("username") String username) {
        return accountService.getAccount(username);
    }

    //登录，根据用户名和密码获取用户信息
    @GetMapping("accounts/username/{username}/password/{password}")
    @ResponseBody
    public CommonResponse<Account> getAccount(@PathVariable("username") String username,@PathVariable("password") String password) {
        return accountService.getAccount(username,password);
    }

    //根据手机号获取用户信息
    @GetMapping("accounts/{phone}/phone")
    @ResponseBody
    public CommonResponse<Account> getAccountByPhone(@PathVariable("phone") String phone) {
        return accountService.getAccountByPhone(phone);
    }


    //插入一个用户到数据库
    @PostMapping("accounts")
    @ResponseBody
    public CommonResponse<Account> insertAccount(@RequestBody Account account) {
        return accountService.insertAccount(account);
    }


    //更新用户信息
    @PutMapping("accounts")
    @ResponseBody
    public CommonResponse<Account> updateAccount(@RequestBody Account accountt) {
        return accountService.updateAccount(accountt);
    }


    //判断是否已存在该用户
    @GetMapping("accounts/{username}")
    @ResponseBody
    public CommonResponse<String> isExit(@PathVariable("username") String username) {
        return accountService.isExit(username);
    }


    //返回用户类型
    @GetMapping("accounts/identity/{username}")
    @ResponseBody
    public CommonResponse<String[]> getIdentity(@PathVariable("username") String username) {
        return accountService.getIdentity(username);
    }


    //获取所有用户
    @GetMapping("accounts")
    @ResponseBody
    public CommonResponse<List<Account>> getAllAccount() {
        return accountService.getAllAccount();
    }

    //申请成为商家，插入一个记录到undeterminedfarm
    @PostMapping("accounts/business")
    @ResponseBody
    public CommonResponse<UnDeterminedFarms> insertUndeterminedFarm(@RequestBody UnDeterminedFarms unDeterminedFarms) {
        return accountService.insertUndeterminedFarm(unDeterminedFarms);
    }

    //若该用户的商家，获取个人信息和他的农场信息
    @GetMapping("accounts/farm/{username}/username")
    CommonResponse<Business> getBusiness(@PathVariable("username") String userName) {
        return accountService.getBusiness(userName);
    }

}
